﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockBusiness;
using MockCommon;
using MockDataTransferObject;
using System.Globalization;

namespace Presentation.AdminPresentation
{
    public partial class UpdateContract : System.Web.UI.Page
    {
        private ContractBusiness _contractBusiness;
        private ConsultantBusiness _consultantBusiness;
        private ContractTypeBusiness _contractTypeBusiness;
        private ClientBusiness _clientBusiness;
        private Label _warningLabel;
        private Label _errorLabel;
        private Label _successLabel;
        private DropDownList _consultantDropDownList;
        private DropDownList _contractTypeDropDownList;
        private DropDownList _clientDropDownList;
        private TextBox _contentTextBox;
        private TextBox _startDateTextBox;
        private TextBox _endDateTextBox;
        private Button _saveButton;
        private Button _backButton;

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                _contractBusiness = ContractBusiness.RetrieveInstance();
                _consultantBusiness = ConsultantBusiness.RetrieveInstance();
                _contractTypeBusiness = ContractTypeBusiness.RetrieveInstance();
                _clientBusiness = ClientBusiness.RetrieveInstance();
                _warningLabel = (Label)Master.FindControl("WarningLabel");
                _errorLabel = (Label)Master.FindControl("ErrorLabel");
                _successLabel = (Label)Master.FindControl("SuccessLabel");
                _consultantDropDownList = (DropDownList)UpdateContractControl.FindControl("ConsultantDropDownList");
                _contractTypeDropDownList = (DropDownList)UpdateContractControl.FindControl("ContractTypeDropDownList");
                _clientDropDownList = (DropDownList)UpdateContractControl.FindControl("ClientDropDownList");
                _contentTextBox = (TextBox)UpdateContractControl.FindControl("ContentTextBox");
                _startDateTextBox = (TextBox)UpdateContractControl.FindControl("StartDateTextBox");
                _endDateTextBox = (TextBox)UpdateContractControl.FindControl("EndDateTextBox");
                _saveButton = (Button)UpdateContractControl.FindControl("SaveButton");
                _backButton = (Button)UpdateContractControl.FindControl("BackButton");
                _saveButton.Click += new EventHandler(SaveButton_Click);
                _backButton.Click += new EventHandler(BackButton_Click);
            }
            catch (MockException exception)
            {
                Application["error"] = exception;
                Response.Redirect("../Error.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null && Session["user"] is User)
            {
                if (Request.QueryString["cid"] == null)
                {
                    Server.Transfer("../NotFound.aspx");
                }
                User user = (User)Session["user"];
                if (user.Role.RoleName == "Admin")
                {
                    if (!IsPostBack)
                    {
                        Loading();
                    }
                }
                else
                {
                    Server.Transfer("../NotAuthorized.aspx");
                }
            }
            else
            {
                Response.Redirect("../LogOn.aspx");
            }
            if (IsPostBack)
            {
                _warningLabel.Text = "";
                _errorLabel.Text = "";
                _successLabel.Text = "";
            }
        }

        private void Loading()
        {
            try
            {
                _clientDropDownList.DataSource = _clientBusiness.RetrieveAllClientUsers();
                _clientDropDownList.DataValueField = "ClientId";
                _clientDropDownList.DataTextField = "Username";
                _clientDropDownList.DataBind();
                _consultantDropDownList.DataSource = _consultantBusiness.RetrieveAllConsultantUsers();
                _consultantDropDownList.DataValueField = "ConsultantId";
                _consultantDropDownList.DataTextField = "Username";
                _consultantDropDownList.DataBind();
                _contractTypeDropDownList.DataSource = _contractTypeBusiness.RetrieveAllContractType();
                _contractTypeDropDownList.DataValueField = "ContractTypeId";
                _contractTypeDropDownList.DataTextField = "ContractTypeName";
                _contractTypeDropDownList.DataBind();
                Guid id = Guid.Parse(Request.QueryString["cid"]);
                MockDataTransferObject.Contract contract = _contractBusiness.GetById(id);
                _consultantDropDownList.SelectedValue = contract.Consultant.ConsultantId.ToString();
                _consultantDropDownList.SelectedValue = contract.ContractType.ContractTypeId.ToString();
                _clientDropDownList.SelectedValue = contract.Client.ClientId.ToString();
                _contentTextBox.Text = contract.Content;
                _startDateTextBox.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", contract.StartDate);
                _endDateTextBox.Text = String.Format(CultureInfo.CurrentCulture, "{0:MM/dd/yyyy}", contract.EndDate);
            }
            catch (NullReferenceException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            try
            {  
                //consultant
                Guid consultantId = Guid.Parse(_consultantDropDownList.Text);
                Consultant consultant = _consultantBusiness.GetById(consultantId);          
                //contract type
                Guid contractTypeId = Guid.Parse(_contractTypeDropDownList.Text);
                ContractType contractType = _contractTypeBusiness.GetById(contractTypeId);              
                //client
                Guid clientId = Guid.Parse(_clientDropDownList.Text);
                Client client = _clientBusiness.GetById(clientId);
                //contract
                Guid id = Guid.Parse(Request.QueryString["cid"]);
                MockDataTransferObject.Contract contract = _contractBusiness.GetById(id);
                contract.ContractId = id;
                contract.Client = client;
                contract.Consultant = consultant;
                contract.ContractType = contractType;
                contract.Content = _contentTextBox.Text;
                contract.StartDate = DateTime.Parse(_startDateTextBox.Text, CultureInfo.CurrentCulture);
                contract.EndDate = DateTime.Parse(_endDateTextBox.Text, CultureInfo.CurrentCulture);
                //update
                _contractBusiness.Update(contract);
                _successLabel.Text = "Update contract successfully!";
            }
            catch (NullReferenceException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (ArgumentException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (FormatException exception)
            {
                _errorLabel.Text = exception.Message;
            }
            catch (MockException exception)
            {
                _errorLabel.Text = exception.ExceptionMessage;
            }
        }

        protected void BackButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListContract.aspx");
        }
    }
}